#============================================================
#	Item
#============================================================
# @datetime: 2022-4-20 23:23:08
#============================================================
extends MarginContainer


signal selected(item_data)


var _data : Dictionary
var _directory := Directory.new()


#============================================================
#   Set/Get
#============================================================
func set_data(value: Dictionary):
	_data = value
	var icon : String = _data.get('icon', "")
	if icon == "":
		icon = _data.get("path", "res://icon.png")
	if not _directory.file_exists(icon):
		icon = "res://src/assets/pixel/items/weapon/".plus_file(icon.get_file())
		if not _directory.file_exists(icon):
			icon = "res://src/assets/pixel/items/potion/".plus_file(icon.get_file())
	
	set_texture(load(icon))

func set_texture(texture: Texture):
	if not is_inside_tree():
		yield(self, "ready")
	$TextureRect.texture = texture

func get_data() -> Dictionary:
	return _data


#============================================================
#   内置
#============================================================
func _gui_input(event):
	if event is InputEventMouseButton:
		if event.button_index == BUTTON_LEFT and event.pressed:
			emit_signal("selected", _data)
	elif event is InputEventScreenTouch:
		emit_signal("selected", _data)



